/*
 * Author: nicolas.bredeche(@)lri.fr
 * Created on 7 janv. 2006
 * 
 */

package picoevo.tutorials.ga.maxone;

import picoevo.core.evolution.ParameterSet;
import picoevo.core.representation.World;
import picoevo.ext.evolution.InitialisationOperator_Element_StaticArray_Bit;
import picoevo.ext.evolution.InitialisationOperator_Individual_StaticArray_Bit;
import picoevo.ext.evolution.InitialisationOperator_Population_SimplePopulation;
import picoevo.ext.evolution.SelectionOperator_MuLambda;
import picoevo.ext.evolution.StatisticsOperator_Population;
import picoevo.ext.representation.Population_SimplePopulation;
import picoevo.toolbox.Dictionary;
import picoevo.toolbox.Display;
import picoevo.toolbox.Misc;
import picoevo.toolbox.Version;

public class GeneticAlgorithm_MaxOne {
	public static void launchExampleOne_MaxOne_GAstyle() {
		Display.info("#### MaxOne Tutorial (\"fastest\" GA-like version) ####");

		// 1 - create an Evolution environment along with a single embedded
		// Population

		ParameterSet parameterSet = new ParameterSet();

		parameterSet.setProperty("GenomeSize", 512);
		parameterSet.setProperty("Mu", 5);
		parameterSet.setProperty("Lambda", 195);
		parameterSet.setProperty("MutationRate", 1.0 / 512.0);
		parameterSet.setProperty("MuPlusLambda", true);
		parameterSet.setProperty("Generations", 250);
		parameterSet.setProperty("InitPopSize", 200);
		parameterSet.setProperty("OptimisationFlag", Boolean.toString(Dictionary.Maximisation));

		// parameterSet.put("SelectionOperator",new
		// SelectionOperator_Koza92multiplexer("Koza92multiplexer")); // testing
		parameterSet.setProperty("SelectionOperator", new SelectionOperator_MuLambda("MuLambda"));
		parameterSet.setProperty("EvaluationOperator_Individual", new EvaluationOperator_MaxOne_StaticArray_Bit("MaxOne"));
		parameterSet.setProperty("PopulationInitialisationOperator", new InitialisationOperator_Population_SimplePopulation("MaxOne"));
		parameterSet.setProperty("IndividualInitialisationOperator", new InitialisationOperator_Individual_StaticArray_Bit("MaxOne"));
		parameterSet.setProperty("ElementInitialisationOperator", new InitialisationOperator_Element_StaticArray_Bit());
		parameterSet.setProperty("PopulationStatisticsOperator", new StatisticsOperator_Population());

		parameterSet.setProperty("LogFilename", "log/logfile_maxOne_" + Misc.getCurrentTimeAsCompactString() + ".data");

		parameterSet.check();
		parameterSet.displayInformation();

		Display.info("\n\n## initialization ##");

		World myWorld = new World("myEvolution", parameterSet);
		Population_SimplePopulation maxOnePop = new Population_SimplePopulation("max-one population", myWorld);
		myWorld.registerPopulation(maxOnePop);

		maxOnePop.performInitialisation();

		// maxOnePop.displayInformation();

		// 2 - evolution

		Display.info("\n\n## evolving ##");

		int displayFreq = 10;

		for (int i = 0; i != myWorld.getTemplate().getIntProperty("Generations"); i++) {
			if (i % displayFreq == 0)
				myWorld.evolveOneStep(true);
			else
				myWorld.evolveOneStep(false);
		}

		// maxOnePop.displayInformation();

		Display.info("\n\n\n## terminating ##\n");
	}

	public static void main(String[] args) {
		double startTime = System.currentTimeMillis();

		Version.displayCurrentReleaseInformation();

		Display.info("###PicoEvo Tutorial Example###");
		Display.info("Running...");
		launchExampleOne_MaxOne_GAstyle();
		Display.info("\nTerminated (" + ((System.currentTimeMillis() - startTime) / 1000) + "s elapsed).");
	}

}
